package com.hy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.handler.BusinessException;
import com.hy.response.Result;
import com.hy.system.entity.User;
import com.hy.system.service.UserService;
import com.hy.viewObject.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-04-06
 */
@RestController
@RequestMapping("/system/user")
@Api(value = "系统用户模块",tags = "系统用户接口")
//@CrossOrigin  //单独使用注释 解决跨域问题  ，但是没用 不知道为啥
public class UserController {

    @Resource
    UserService userService;

    /**
     * 分页查询用户列表
     *
     * @return
     */
    @ApiOperation("分页查询用户列表")
    @GetMapping("/findUserList")
    public Result findUserList(@RequestParam(required = true, defaultValue = "1") Integer current,
                               @RequestParam(required = true, defaultValue = "6") Integer size) {
        //对用户进行分页,泛型中注入的就是用户实体类
        Page<User> page = new Page<>(current, size);
        //单表的时候其实这个方法是非常好用的
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        Page<User> userPage = userService.page(page);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 按条件分页高级联表查询用户列表
     *
     * @return
     */
    @ApiOperation("按条件分页高级联表查询用户列表")
    @PostMapping("/findUserPage")
    public Result findUserPage(@RequestParam(required = true, defaultValue = "1") Integer current, //@RequestParam前端用params：以Url中？形式传递参数给后端
                               @RequestParam(required = true, defaultValue = "6") Integer size,
                               @RequestBody UserVO userVo) {                                      //@RequestBody前端用data：以Json 数据传递参数给后端
        //对用户进行分页,泛型中注入的就是用户实体类
        Page<User> page = new Page<>(current, size);
        QueryWrapper<User> userQueryWrapper = getUserWrapper(userVo);//在下面封装getUserWrapper()
        IPage<User> userPage = userService.findUserPage(page, userQueryWrapper);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 封装查询条件
     *
     * @param userVo
     * @return
     */
    private QueryWrapper<User> getUserWrapper(UserVO userVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (userVo != null) {
            if (!StringUtils.isEmpty(userVo.getDepartmentId())) {
                queryWrapper.eq("department_id", userVo.getDepartmentId());
            }
            if (!StringUtils.isEmpty(userVo.getNickname())) {
                queryWrapper.eq("nickname", userVo.getNickname());
            }
            if (!StringUtils.isEmpty(userVo.getTel())) {
                queryWrapper.eq("tel", userVo.getTel());
            }
            if (!StringUtils.isEmpty(userVo.getSex())) {
                queryWrapper.eq("sex", userVo.getSex());
            }
            if (!StringUtils.isEmpty(userVo.getUsername())) {
                queryWrapper.eq("username", userVo.getUsername());
            }
        }
        return queryWrapper;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @ApiOperation(value = "添加用户",notes = "添加用户信息")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        try {
            userService.addUser(user);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            return Result.error();
        }
    }

    /**
     * poi导出Excel
     * @return
     */
    @ApiOperation(value = "poi导出高版本Excel",notes = "poi导出用户数据Excel")
    @GetMapping("/downLoadXlsxByPoi")
    public Result downLoadXlsxByPoi(HttpServletResponse response) throws Exception{
        try {
            userService.downLoadXlsxByPoi(response);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }

}

